package com.web.controller.web;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.web.dao.CarouselMapper;
import com.web.dao.StoreMapper;
import com.web.domain.WebResponse;
import com.web.domain.dataTable.BootstrapTableColumns;
import com.web.domain.dataTable.BootstrapTableResponse;
import com.web.entity.Carousel;
import com.web.entity.Store;
import com.web.manager.FileUpLoadManager;
import com.xkk.utils.UUIDUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author FDjavaone
 * @date 2017/9/26
 */

@Slf4j
@Controller
public class WebCarouselController {
    @Autowired
    private CarouselMapper carouselMapper;
    @Autowired
    private FileUpLoadManager fileUpLoadManager;
    @Autowired
    private StoreMapper storeMapper;


    @RequestMapping(value = "sysm/cycle",
            method = RequestMethod.GET)
    public String cycle(Model model, HttpServletRequest request) {
        return "carousel";
    }

    @Data
    public static class CarouselColumns extends BootstrapTableColumns {
        private String activityName;
        private String status;
        private String storeName;
        private String type;
    }

    @GetMapping("/sysm/getCarousel")
    @ResponseBody
    public BootstrapTableResponse getCarousel(CarouselColumns carouselColumns) {
        PageHelper.startPage(carouselColumns.getPageNumber(), carouselColumns.getPageSize());
        if (carouselColumns.getActivityName() != null && !"".equals(carouselColumns.getActivityName())) {
            carouselColumns.setActivityName("%" + carouselColumns.getActivityName() + "%");
        }
        if (carouselColumns.getStoreName() != null && !"".equals(carouselColumns.getStoreName())) {
            carouselColumns.setStoreName("%" + carouselColumns.getStoreName() + "%");
        }
        Page<Carousel> page = carouselMapper.selectAllCarousel(carouselColumns);
        BootstrapTableResponse bootstrapTableResponse = new BootstrapTableResponse();
        bootstrapTableResponse.setResult(page.getResult());
        bootstrapTableResponse.setTotalCount(page.getTotal());
        return bootstrapTableResponse;
    }

    @RequestMapping("/sysm/file")
    @ResponseBody
    public Map fileUpload(HttpServletRequest request,
                          @RequestParam("file") CommonsMultipartFile file) {
        Map map = new HashMap();
        //获取输入流 CommonsMultipartFile 中可以直接得到文件的流
        try {
            String str = "";
            InputStream is = file.getInputStream();
            String fileName = file.getOriginalFilename();
            str = fileUpLoadManager.upload(is, fileName);
            map.put("message", "上传成功！");
            map.put("data", str);
            map.put("code", 900000);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("message", e.getMessage());
            map.put("code", 900004);
        }
        return map;
    }

    @RequestMapping(value = "/sysm/carousel/add")
    @ResponseBody
    public WebResponse add(Carousel carousel, MultipartFile file) {
        Date date = new Date();
        try {
            InputStream is = file.getInputStream();
            String str = fileUpLoadManager.upload(is, UUIDUtil.getUUID() + file.getOriginalFilename());
            carousel.setActivityPicture("http://" + str);
            carousel.setModifyTime(date);
            carousel.setCreateTime(date);
            carouselMapper.insertSelective(carousel);
            return WebResponse.ok("添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return WebResponse.fail(WebResponse.CF);
        }
    }

    @RequestMapping(value = "/sysm/carousel/addStore")
    @ResponseBody
    public WebResponse addStore(Carousel carousel, MultipartFile file) {
        Date date = new Date();
        try {
            String[] storeArr = carousel.getStoreName().split("\\|");
            if (storeArr.length == 1) {
                return WebResponse.fail("店铺信息有误");
            }
            carousel.setStoreName(storeArr[1]);
            carousel.setGoodsPicture(storeArr[0]);
            Store store = storeMapper.selectByPrimaryKey(Long.parseLong(storeArr[0]));
            carousel.setActivityName(store.getType());
            InputStream is = file.getInputStream();
            String str = fileUpLoadManager.upload(is, UUIDUtil.getUUID() + file.getOriginalFilename());
            carousel.setActivityPicture("http://" + str);
            carousel.setModifyTime(date);
            carousel.setType("店铺");
            carousel.setCreateTime(date);
            carouselMapper.insertSelective(carousel);
            return WebResponse.ok("添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return WebResponse.fail(WebResponse.CF);
        }
    }


    @RequestMapping("/sysm/carousel/refuse")
    @ResponseBody
    public WebResponse refuse(Carousel carousel) {
        Date date = new Date();
        Carousel old = carouselMapper.selectByPrimaryKey(carousel.getId());
        if (old.getStatus() == 1) {
            carousel.setStatus(0);
            carousel.setModifyTime(date);
            carouselMapper.updateByPrimaryKeySelective(carousel);
            return WebResponse.ok("操作成功");
        } else {
            return WebResponse.fail("操作失败");
        }
    }

    @RequestMapping("/sysm/carousel/delete")
    @ResponseBody
    public WebResponse delete(Carousel carousel) {
        carouselMapper.deleteByPrimaryKey(carousel.getId());
        return WebResponse.ok("操作成功");
    }

    @RequestMapping("/sysm/carousel/pass")
    @ResponseBody
    public WebResponse pass(Carousel carousel) {
        Date date = new Date();
        Carousel old = carouselMapper.selectByPrimaryKey(carousel.getId());
        if (old.getStatus() == 0) {
            carousel.setStatus(1);
            carousel.setModifyTime(date);
            carouselMapper.updateByPrimaryKeySelective(carousel);
            return WebResponse.ok("操作成功");
        } else {
            return WebResponse.fail("操作失败");
        }
    }


    @RequestMapping("/sysm/carousel/getDetail")
    @ResponseBody
    public WebResponse getDetail(Carousel carousel) {
        Carousel old = carouselMapper.selectByPrimaryKey(carousel.getId());
        Map map = Maps.newHashMap();
        map.put("detail", old);
        return WebResponse.ok("获取成功", map);

    }


    @RequestMapping(value = "/sysm/carousel/edit")
    @ResponseBody
    public WebResponse edit(Carousel carousel, MultipartFile file) {
        Date date = new Date();
        try {
            InputStream is = file.getInputStream();
            int size = is.available();
            if (size == 0) {
                log.info("流为空，不操作");
            } else {
                String str = fileUpLoadManager.upload(is, UUIDUtil.getUUID() + file.getOriginalFilename());
                carousel.setActivityPicture("http://" + str);
            }
            carousel.setType("h5");
            carousel.setStoreName("");
            carousel.setModifyTime(date);
            carouselMapper.updateByPrimaryKeySelective(carousel);
            return WebResponse.ok("编辑成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return WebResponse.fail(WebResponse.CF);
        }
    }

    @RequestMapping(value = "/sysm/carousel/editStore")
    @ResponseBody
    public WebResponse editStore(Carousel carousel, MultipartFile file) {
        Date date = new Date();
        try {
            String[] storeArr = carousel.getStoreName().split("\\|");
            if (storeArr.length == 1) {
                return WebResponse.fail("店铺信息有误");
            } else {
                carousel.setStoreName(storeArr[1]);
                carousel.setGoodsPicture(storeArr[0]);
                Store store = storeMapper.selectByPrimaryKey(Long.parseLong(storeArr[0]));
                carousel.setActivityName(store.getType());
            }
            InputStream is = file.getInputStream();
            int size = is.available();
            if (size == 0) {
                log.info("流为空，不操作");
            } else {
                String str = fileUpLoadManager.upload(is, UUIDUtil.getUUID() + file.getOriginalFilename());
                carousel.setActivityPicture("http://" + str);
            }
            carousel.setType("店铺");
            carousel.setModifyTime(date);
            carousel.setActivityName("");
            carousel.setActivityUrl("");
            carouselMapper.updateByPrimaryKeySelective(carousel);
            return WebResponse.ok("编辑成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return WebResponse.fail(WebResponse.CF);
        }
    }

}
